// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    try {
        // 存储数据
        let data = {
            ...event.data
        }

        // 日期格式化
        const date = new Date(data.createTime)
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
        const day = String(date.getDate()).padStart(2, '0');
        // 时、分、秒
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        data.createTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`

      
        const res = db.collection('couponList').add({
            data
        })

        if (res.errMsg = "collection.add:ok") {
            return {
                code: 200,
                msg: "新增成功",
            }
        } else {
            return {
                code: 401,
                data,
                msg: "新增失败",
            }
        }
    } catch {
        return {
            code: 401,
            data,
            msg: "新增失败",
        }
    }
}